package com.atguigu.guli.service.statistics.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.statistics.service.DailyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author uuei
 * @since 2021-07-20
 */
//@CrossOrigin
@Api(tags = "统计分析管理")
@RestController
@RequestMapping("/admin/statistics/daily")
@Slf4j
public class DailyController {

    @Autowired
    private DailyService dailyService;

    @ApiOperation("生成记录统计")
    @PostMapping("create/{day}")
    public R createStatisticsByDay(
            @ApiParam("统计日期")
            @PathVariable String day){
        dailyService.createStatisticsByDay(day);
        return R.ok().message("数据统计生成成功");
    }

    @ApiOperation("显示统计数据")
    @GetMapping("show-chart/{begin}/{end}")
    public R showChart(
            @ApiParam(value = "开始时间")@PathVariable String begin,
            @ApiParam(value = "结束时间")@PathVariable String end){
        Map<String, Map<String,Object>> map = dailyService.getChartData(begin,end);
        return R.ok().data("chartData",map);
    }

    /**
     * 每天凌晨1点执行定时任务
     */
    @Scheduled(cron = "0 0 1 * * ?")//只支持6位表达式
    public void taskGenarateStatisticsData(){
        //获取一天的日期
        String day = new DateTime().minusDays(1).toString("yyyy-MM-dd");
        dailyService.createStatisticsByDay(day);
        log.info("taskGenarateStatisticsData 统计完毕");
    }
}

